package com.itheima.service.impl;

import com.itheima.dao.QuestionItemDao;
import com.itheima.domain.QuestionItem;
import com.itheima.service.QuestionItemService;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class QuestionItemServiceImpl implements QuestionItemService {
    @Override
    public void delete(String questionId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        QuestionItemDao mapper = sqlSession.getMapper(QuestionItemDao.class);
        mapper.delete(questionId);
        sqlSession.close();
    }

    @Override
    public List<QuestionItem> findAll(String questionId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        QuestionItemDao mapper = sqlSession.getMapper(QuestionItemDao.class);
        List<QuestionItem> list = mapper.findAll(questionId);
        sqlSession.close();
        return list;
    }

    @Override
    public void update(String questionId, List<QuestionItem> list) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        QuestionItemDao mapper = sqlSession.getMapper(QuestionItemDao.class);
        mapper.delete(questionId);
        for (QuestionItem questionItem : list) {
            questionItem.setQuestionId(questionId);
            String id = UUID.randomUUID().toString();
            questionItem.setId(id);
            mapper.update(questionItem);
        }
        sqlSession.close();
    }
}
